﻿package org.neelts.web.vkontakte 
{
	import flash.display.DisplayObject;
	import flash.display.LoaderInfo;
	
	/**
	* ...
	* @author Neelts © 2008
	*/
	public class APIVars
    {
        public static var instance:APIVars;
		
		private var _api_id:String = "";
		private var _user_id:String = "";
		private var _group_id:String = "";
		private var _viewer_id:String = "";
		private var _viewer_type:String = "";
		
		private var _appType:String = "";
		
		public static const APP_USER:String = "app_user";
		public static const APP_GROUP:String = "app_group";
		
		public static const NOT_USER_FRIEND:String = "not_user_friend";
		public static const USER_FRIEND:String = "not_user_friend";
		public static const USER:String = "not_user_friend";
		
		public static const NOT_GROUP_MEMBER:String = "not_group_member";
		public static const GROUP_MEMBER:String = "group_member";
		public static const GROUP_HEAD:String = "group_head";
		public static const GROUP_ADMIN:String = "group_admin";
		
        public static function getInstance(root:DisplayObject = null):APIVars
        {
            if( instance == null ) instance = new APIVars( new APIVarsEnforcer(), root );
            return instance;
        }
		
        public function APIVars( ave:APIVarsEnforcer, root:DisplayObject )
        {
			
			if (root == null) {
				throw new Error("APIVars: root parameter is null");
				return;
			}
			
			var vars:Object = LoaderInfo(root.loaderInfo).parameters;
			
			_api_id = vars.api_id;
			_user_id = vars.user_id;
			_group_id = vars.group_id;
			_viewer_id = vars.viewer_id;
			_viewer_type = vars.viewer_type;
			
			if (_user_id != "0") {
				
				switch (_viewer_type) {
					case "0":
						_viewer_type = NOT_USER_FRIEND;
					break;
					case "1":
						_viewer_type = USER_FRIEND;
					break;
					case "2":
						_viewer_type = USER;
					break;
				}
				_appType = APP_USER;
				
			} else if (_group_id != "0") {
				
				switch (_viewer_type) {
					case "0":
						_viewer_type = NOT_GROUP_MEMBER;
					break;
					case "1":
						_viewer_type = GROUP_MEMBER;
					break;
					case "2":
						_viewer_type = GROUP_HEAD;
					break;
					case "3":
						_viewer_type = GROUP_ADMIN;
					break;
				}
				_appType = APP_GROUP;
			}
			
        }
		
		public function get api():String { return _api_id; }
		
		public function get user():String { return _user_id; }
		
		public function get group():String { return _group_id; }
		
		public function get viewer():String { return _viewer_id; }
		
		public function get viewerType():String { return _viewer_type; }
		
		public function get appType():String { return _appType; }
    }
}

internal class APIVarsEnforcer{}